package com.guoliang.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.guoliang.commonUtils.CommonResult;
import com.guoliang.service.exceptionHandler.MyException;
import com.guoliang.vod.service.VodService;
import com.guoliang.vod.utils.AliyunVodSDKUtils;
import com.guoliang.vod.utils.ConstantUtil;
import io.swagger.annotations.ApiOperation;
import org.hamcrest.core.CombinableMatcher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author ：Leo
 * @date ：Created in 2023/1/24 11:49
 * @description：
 */
@RestController
@RequestMapping("/eduvod/video")
//@RequestMapping
public class VodController {
    @Autowired
    private VodService vodService;
    @ApiOperation("上传视频")
    @PostMapping
    public CommonResult uploadVideo(MultipartFile file){
        String videoId=vodService.uploadVideo(file);
        return CommonResult.ok().data("id",videoId);
    }
    @ApiOperation("删除视频")
    @DeleteMapping("{id}")
    public CommonResult deleteVod(@PathVariable("id")String id){
        vodService.deleteVideo(id);
        return CommonResult.ok();
    }
    @ApiOperation("删除多个视频")
    @DeleteMapping("deleteBatch")
    public CommonResult deleteBatch(@RequestParam("videoIdList") List videoIdList){
        vodService.deleteBatch(videoIdList);
        return CommonResult.ok();
    }
    @ApiOperation("根据视频id获取视频播放凭证")
    @GetMapping("getPlayAuth/{id}")
    public CommonResult getPlayAuth(@PathVariable("id")String id){
        try {
            DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(ConstantUtil.ACCESS_KEY_ID
                    , ConstantUtil.ACCESS_KEY_SECRET);
            GetVideoPlayAuthRequest request=new GetVideoPlayAuthRequest();
            request.setVideoId(id);
            GetVideoPlayAuthResponse response = client.getAcsResponse(request);
            return CommonResult.ok().data("playAuth",response.getPlayAuth());
        } catch (ClientException e) {
            throw new MyException(20001,"获取播放凭证失败");
        }
    }

}
